//
//  ForumTableViewCell.m
//  IT爱好者
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 Fan. All rights reserved.
//

#import "ForumTableViewCell.h"
#import "Header.h"
#import "UIImageView+WebCache.h"
@implementation ForumTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatViews];
    }
    return self;
}
- (void)creatViews
{
    _titleL = [[UILabel alloc]init];
    _titleL.font = contentTextSize;
    _titleL.textColor = contenttextColor;
    _titleL.backgroundColor = BackgroundColor;
    _titleL.numberOfLines = 0;
    [self.contentView addSubview:_titleL];
    
    _goodL = [[UILabel alloc]init];
#pragma mark label做成圆角 需要额外写clipsToBounds属性
    _goodL.layer.cornerRadius = 3;
    _goodL.clipsToBounds = YES;
    _goodL.backgroundColor = [UIColor redColor];
    _goodL.textColor = [UIColor whiteColor];
    _goodL.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_goodL];
    
    
    _pic_urlImViewL = [[UIImageView alloc]init];
    [self.contentView addSubview:_pic_urlImViewL];

    
    _pic_urlImViewM = [[UIImageView alloc]init];
    [self.contentView addSubview:_pic_urlImViewM];

    
    _pic_urlImViewR = [[UIImageView alloc]init];
    [self.contentView addSubview:_pic_urlImViewR];

    
    _photoImView = [[UIImageView alloc]init];
#pragma mark 头像做圆角
    _photoImView.layer.cornerRadius = 10;
    [self.contentView addSubview:_photoImView];

    
    _nicknameL = [[UILabel alloc]init];
    _nicknameL.font = [UIFont systemFontOfSize:12];
    _nicknameL.backgroundColor = BackgroundColor;
    _nicknameL.textColor = contenttextColor;
    _nicknameL.numberOfLines = 0;
    _nicknameL.textColor = otherTextColor;
    [self.contentView addSubview:_nicknameL];

    
    _replyL = [[UILabel alloc]init];
    _replyL.font = [UIFont systemFontOfSize:12];
    _replyL.backgroundColor = BackgroundColor;
    _replyL.textColor = contenttextColor;
    _replyL.numberOfLines = 0;
    [self.contentView addSubview:_replyL];

    
    _lastdateL = [[UILabel alloc]init];
    _lastdateL.font = [UIFont systemFontOfSize:12];
    _lastdateL.backgroundColor = BackgroundColor;
    _lastdateL.numberOfLines = 0;
    [self.contentView addSubview:_lastdateL];

    
    _footView = [[UIView alloc]init];
    _footView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.contentView addSubview:_footView];

    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _titleL.backgroundColor = BackgroundColor;
    _titleL.textColor = contenttextColor;
    _nicknameL.backgroundColor = BackgroundColor;
    _replyL.backgroundColor = BackgroundColor;
    _replyL.textColor = contenttextColor;
    _lastdateL.backgroundColor = BackgroundColor;
    //精华几 自适应长度
#pragma mark 关键字高亮
    if (_ForumAModel.title && _keyWord) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_ForumAModel.title];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        [attributedString setAttributes:dic range:[_ForumAModel.title rangeOfString:_keyWord]];
        _titleL.attributedText = attributedString;
    }
    
    NSDictionary *dicgoodL = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
    CGRect rectOther = [_goodL.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 13 * FitHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicgoodL context:nil];
    //title自适应高度
    NSDictionary *dicTitle = [NSDictionary dictionaryWithObjectsAndKeys:contentTextSize, NSFontAttributeName, nil];
    CGRect recttitle = [_titleL.text boundingRectWithSize:CGSizeMake(ScreenWidth - 50 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicTitle context:nil];
    
    _goodL.frame = CGRectMake(15 * FitWidth, 17 * FitWidth, rectOther.size.width, 13 * FitHeight);
    
#pragma mark 可以顶格x=15 判断有无精 有则空两个格拼接字(以实现)
    _titleL.frame = CGRectMake(15 * FitWidth, _goodL.frame.origin.y - 2 * FitHeight, ScreenWidth - 30 * FitWidth - _goodL.frame.size.width, recttitle.size.height);
    
    //判断数据有无图片 有给高度 无不给高度
    NSInteger i = 0;
    for (NSString *str in _ForumAModel.pic_url) {
        if (![str isEqualToString:@""]) {
            i++;
        }
    }
    if (i >= 1) {
        _pic_urlImViewL.frame = CGRectMake(_goodL.frame.origin.x, _titleL.frame.origin.y + _titleL.frame.size.height + 10 * FitHeight, 105 * FitWidth, 80 * FitHeight);
        _pic_urlImViewM.frame = CGRectMake(_pic_urlImViewL.frame.origin.x + _pic_urlImViewL.frame.size.width + 15 * FitWidth, _pic_urlImViewL.frame.origin.y, _pic_urlImViewL.frame.size.width, _pic_urlImViewL.frame.size.height);
        _pic_urlImViewR.frame = CGRectMake(_pic_urlImViewM.frame.origin.x + _pic_urlImViewM.frame.size.width + 15 * FitWidth, _pic_urlImViewM.frame.origin.y, _pic_urlImViewM.frame.size.width, _pic_urlImViewM.frame.size.height);
    }else{
        _pic_urlImViewL.frame = CGRectMake(_goodL.frame.origin.x, _titleL.frame.origin.y + _titleL.frame.size.height + 10 * FitHeight, 105 * FitWidth, 0);
        _pic_urlImViewM.frame = CGRectMake(_pic_urlImViewL.frame.origin.x + _pic_urlImViewL.frame.size.width + 15 * FitWidth, _pic_urlImViewL.frame.origin.y, _pic_urlImViewL.frame.size.width, 0);
        _pic_urlImViewR.frame = CGRectMake(_pic_urlImViewM.frame.origin.x + _pic_urlImViewM.frame.size.width + 15 * FitWidth, _pic_urlImViewM.frame.origin.y, _pic_urlImViewM.frame.size.width, 0);
    }
    _photoImView.frame = CGRectMake(_pic_urlImViewL.frame.origin.x, _pic_urlImViewL.frame.origin.y + _pic_urlImViewL.frame.size.height + 10 * FitHeight, 20 * FitWidth, 20 * FitHeight);
    _nicknameL.frame = CGRectMake(_photoImView.frame.origin.x + _photoImView.frame.size.width + 10 * FitWidth, _photoImView.frame.origin.y + 5 * FitHeight, 100 * FitWidth, 12 * FitHeight);
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
    CGRect rectLst = [_lastdateL.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 10 * FitHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGRect rectRe = [_replyL.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 10 * FitHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    _lastdateL.frame = CGRectMake(ScreenWidth - 10 * FitWidth - rectLst.size.width, _nicknameL.frame.origin.y, rectLst.size.width, 10 * FitHeight);
    _replyL.frame = CGRectMake(ScreenWidth - 15 * FitWidth - _lastdateL.frame.size.width - 20 * FitWidth - rectRe.size.width, _lastdateL.frame.origin.y, rectRe.size.width, 12 * FitHeight);
    
    _footView.frame = CGRectMake(0, _photoImView.frame.origin.y + _photoImView.frame.size.height + 15 * FitHeight, ScreenWidth, 10 * FitHeight);
    
}

-(void)setForumAModel:(ForumArticleModel *)ForumAModel
{
    if (_ForumAModel != ForumAModel) {
        _ForumAModel = ForumAModel;
        _keyWord = nil;
    }
    if (ForumAModel.good) {
        if ([ForumAModel.good isEqualToString:@"0"]) {
            _goodL.text = nil;
            _titleL.text = ForumAModel.title;
        }else{
            _goodL.text = [NSString stringWithFormat:@"精%@", ForumAModel.good];
            _titleL.text = [NSString stringWithFormat:@"     %@", ForumAModel.title];
        }
    }else{
        _titleL.text = ForumAModel.title;
    }
    
    for (NSInteger i = 0; i < ForumAModel.pic_url.count; i++) {
        if (i == 0) {
            [_pic_urlImViewL sd_setImageWithURL:[NSURL URLWithString:ForumAModel.pic_url[i]] placeholderImage:[UIImage imageNamed:@"123"]];
        }
        if (i == 1) {
            [_pic_urlImViewM sd_setImageWithURL:[NSURL URLWithString:ForumAModel.pic_url[i]] placeholderImage:[UIImage imageNamed:@"123"]];
        }
        if (i == 2) {
            [_pic_urlImViewR sd_setImageWithURL:[NSURL URLWithString:ForumAModel.pic_url[i]] placeholderImage:[UIImage imageNamed:@"123"]];
        }
    }
    ////NSLog(@"%@--%@--%@--%@--%@", ForumAModel.nickname, ForumAModel.reply, ForumAModel.lastdate, ForumAModel.wdate, ForumAModel.bbsname);
    if (ForumAModel.photo) {
        [_photoImView sd_setImageWithURL:[NSURL URLWithString:ForumAModel.photo]];
    }else{
        _photoImView.image = [UIImage imageNamed:@"hongxin"];
    }
    
    _nicknameL.text = ForumAModel.nickname;
    if (ForumAModel.reply != 0) {
        _replyL.text = [NSString stringWithFormat:@"%@ 回", ForumAModel.reply];
    }else{
        _replyL.text = @"抢沙发";
    }
    
#warning 转化为一小时前
    _lastdateL.text = [ForumAModel.lastdate substringFromIndex:11];
    
}

//#pragma mark 搜索的关键字高亮
//-(void)setKeyWord:(NSString *)keyWord
//{
//    if (_keyWord != keyWord) {
//        [_keyWord release];
//        _keyWord = [keyWord retain];
//    }
//    if (_ForumAModel.title) {
//        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_ForumAModel.title];
//        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:15], NSFontAttributeName, nil];
//        [attributedString setAttributes:dic range:[_ForumAModel.title rangeOfString:_keyWord]];
//        _titleL.attributedText = attributedString;
//    }
//    
//}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
